package javaweb.oa;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import javaweb.oa.bean.Dept;
import javaweb.oa.Utils.DBUtil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet({"/dept/list","/dept/detail","/dept/delete","/dept/add","/dept/save"})
public class DeptServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String servletPath = request.getServletPath();

            if ("/dept/list".equals(servletPath)) {
                doList(request, response);
            } else if ("/dept/detail".equals(servletPath)) {
                doDetail(request, response);
            } else if ("/dept/delete".equals(servletPath)) {
                doDelete(request, response);
            } else if ("/dept/add".equals(servletPath)) {
                doAdd(request, response);
            } else if ("/dept/save".equals(servletPath)) {
                doSave(request, response);
            } else{
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }

    }

    /**
     * 修改后保存
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void doSave(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{
        String dname = request.getParameter("dname");
        String loc = request.getParameter("loc");
        String deptno = request.getParameter("deptno");

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        int count = 0;

        try{
            conn = DBUtil.getConnection();
            String sql = "update dept set dname=?, loc=? where deptno = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,dname);
            ps.setString(2,loc);
            ps.setString(3,deptno);
            count = ps.executeUpdate();

        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            DBUtil.close(conn,ps,rs);
        }
        if(count!=0){
            response.sendRedirect(request.getContextPath() + "/dept/list");
        }else{
            response.sendRedirect(request.getContextPath() + "/error.html");
        }

    }

    /**
     * 添加部门
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    private void doAdd(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{
        String deptno = request.getParameter("deptno");
        String dname = request.getParameter("dname");
        String loc = request.getParameter("loc");

        int count = 0;

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            conn = DBUtil.getConnection();
            String sql = "insert into dept(deptno,dname,loc) values(?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1,deptno);
            ps.setString(2,dname);
            ps.setString(3,loc);
            count = ps.executeUpdate();

        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            DBUtil.close(conn,ps,rs);
        }
        if(count!=0){
            response.sendRedirect(request.getContextPath() + "/dept/list");
        }else{
            response.sendRedirect(request.getContextPath() + "/error.html");
        }
    }

    /**
     * 删除部门
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    public void doDelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String deptno = request.getParameter("deptno");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        int count = 0;

        try{
            conn = DBUtil.getConnection();
            String sql = "delete from dept where deptno=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,deptno);
            count = ps.executeUpdate();


        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            DBUtil.close(conn,ps,rs);
        }
        if(count!=0){
            response.sendRedirect(request.getContextPath() + "/dept/list");
        }else{
            response.sendRedirect(request.getContextPath() + "/error.html");
        }

    }

    /**
     * 部门详情
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void doDetail(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{
        String deptno = request.getParameter("deptno");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        Dept dept = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "select dname,loc from dept where deptno=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,deptno);
            rs = ps.executeQuery();
            while(rs.next()){
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");
                dept = new Dept(deptno,dname,loc);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            DBUtil.close(conn,ps,rs);
        }
        request.setAttribute("dept",dept);
        String flag = request.getParameter("flag");
        if(flag.equals("f")){
            request.getRequestDispatcher("/detail.jsp").forward(request,response);
        }else{
            request.getRequestDispatcher("/edit.jsp").forward(request,response);
        }

    }

    /**
     * 连接数据库，查询所有的所有信息，将部门信息收集好，然后跳转到jsp做页面展示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    private void doList(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{



        List<Dept> depts = new ArrayList<>();

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            conn = DBUtil.getConnection();
            String sql = "select deptno,dname,loc from dept";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                String deptno = rs.getString("deptno");
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");
                depts.add(new Dept(deptno,dname,loc));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBUtil.close(conn,ps,rs);
        }
        request.setAttribute("dept",depts);

        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }
}
